<template>
    <div class="equipInfo-container">
        <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
            <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6" class="mb20 search-actions">
                        <div>
                            <el-button type="primary" plain icon="ele-Search" @click="handleQuery" > 查询 </el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-card shadow="hover" style="margin-top: 8px">
            <el-table :data="tableData"	style="width: 100%"	v-loading="loading"	tooltip-effect="light" row-key="id"	border="">
                <el-table-column type="index" label="序号" width="55"  fixed="" align="center" />
                <el-table-column prop="dataType" label="数据类型" fixed="" show-overflow-tooltip="" align="center"/>
                <el-table-column prop="interval" label="发布间隔" fixed="" show-overflow-tooltip="" align="center"/>
                <el-table-column prop="isPublish" label="是否发布" fixed="" show-overflow-tooltip="" align="center">
                    <template #default="scope">
                        <el-switch 
                            disabled
                            v-model="scope.row.isPublish"
                            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus( $event,scope.row.dataType)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('dataType:edit')">
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditDataType(scope.row)" v-auth="'dataType:edit'"> 编辑 </el-button>
					</template>
				</el-table-column>
            </el-table>
            <el-pagination
                    v-model:currentPage="tableParams.page"
                    v-model:page-size="tableParams.pageSize"
                    :total="tableParams.total"
                    :page-sizes="[100]"
                    small=""
                    background=""
                    layout="total, sizes, prev, pager, next, jumper"/>
            <editDialog
			    ref="editDialogRef"
			    :title="editDataTypeTitle"
			    @reloadTable="handleQuery"
      />
        </el-card>
    </div>
    
</template>

<script lang="ts" setup>
    import { ref } from "vue";
    import { ElMessageBox, ElMessage } from "element-plus";
    import { auth } from '/@/utils/authFunction';
    //import { formatDate } from '/@/utils/formatTime';

    import { updateMqttDataPublish,getDataTypes } from '/@/api/main/mqtt';
    import editDialog from '/@/views/main/mqttDataManage/component/editDialog.vue'

    const isShow=ref(false);
    const editDialogRef = ref();
    const loading = ref(false);
    const tableData = ref<any>([]);
    const queryParams = ref<any>({});
        const tableParams = ref({
        page: 1,
        pageSize: 100,
        total: 0,
    });
    const editDataTypeTitle = ref("");
    // 查询操作
    const handleQuery = async () => {
        
            loading.value = true;
            var res = await getDataTypes();
            tableParams.value.total = res.data.result?.total;
           
            tableData.value = res.data.result?.items ?? [];
           
            loading.value = false;
        
    };
   
    const changeStatus = async ($event: boolean,dataType : any ) =>{

        queryParams.value = {dataType : dataType, isPublish : $event};
       
        var res = await updateMqttDataPublish(Object.assign(queryParams.value));
        //if(res.data.result.commandResult == true && res.data.result.started == true){
            //mqttStatus.value="启动";
            //mqttStatusColor.value = "success";
        //}
        handleQuery();
    }
    
    // 打开编辑页面
    const openEditDataType = (row: any) => {
        editDataTypeTitle.value = '编辑发布数据类型';
        editDialogRef.value.openDialog(row);
    };
    handleQuery();

</script>
